// pages/search_lxj/search_lxj.js
const db_lxj = wx.cloud.database()
let searchKey_lxj = ''
Page({
  data: {
    totalMoney:0,
    totalNum: 0,
    cartList: [],
    isHide: true
  },
  onLoad(options) {
    console.log("options",options);
    let searchKey_lxj = options.searchKey_lxj
    db_lxj.collection('food').where({
      fenlei: db_lxj.RegExp({
        regexp: searchKey_lxj,
        options: 'i',
      })
    }).get()
    .then(res=>{
      console.log('搜索成功',res);
      let search_data = res.data
      if(search_data && search_data.length > 0 ){ 
        search_data.forEach(item=>{
          if(cartList && cartList.length>0){
            let result = cartList.find(cart=>{
              return cart._id == item._id
            })
            if(result){
              item.num = result.num
            }else{
              item.num = 0
            }
          }else{
            item.num = 0
          }
        })
        this.setData({
          foodList:search_data,
        })
        this.getTotal()
      }
    })
    .catch(res=>{
      console.log('搜索失败',res);
    })
    
    let cartList = wx.getStorageSync('cart') || []
    this.setData({
      cartList:cartList
    })
  },
   //获取用户输入的内容
   getSearchKey_lxj(e){
    console.log(e.detail.value);
      searchKey_lxj = e.detail.value
    },
    //触发搜索事件
    goSearch_lxj(e){
      console.log('触发搜索事件',searchKey_lxj);
      if(searchKey_lxj&&searchKey_lxj.length>0){ 
        //搜索触发写在这里
       wx.navigateTo({
         url: '/pages/search_lxj/search_lxj?searchKey_lxj=' + searchKey_lxj,
       })
      }else{
        wx.showToast({
          icon:'error',
          title: '请输入关键词'
        })
      }
    },
    //计算总价格和总数量
    getTotal(){
      let cartList = this.data.cartList
      let totalMoney = 0
      let totalNum = 0
      cartList.forEach(item=>{
        totalMoney+=item.price*item.num
        totalNum+=item.num
      })
      this.setData({
        totalMoney,
        totalNum
      })
    },
    //点击增减菜品
    subNum(e){
      let id = e.currentTarget.dataset.id
      console.log('减',id);
      let list = this.data.foodList
      let  cartList = this.data.cartList
      list.forEach(item=>{
        console.log('便利了',item.name);
        if(item._id == id){
          if(item.num > 0)
          {
            item.num --
            var index = cartList.findIndex(cart=>{   //find方法 查询购物车数组cartList里面是否有当前点击的菜品
                return cart._id == id;
              })
              if(index > -1){
                cartList[index].num = item.num
              }
              // console.log('点击的菜品是否在购物车里',index);
              if(item.num == 0){  //当菜品数量为0时，移除
                cartList.splice(index,1) //删除下标为index的元素
              }
          }
          else{
            wx.showToast({
              icon:'none',
              title: '已经不能再减啦！',
            })
          }
        }
      })
      console.log('当前菜品列foodList',list)
      this.setData({
        foodList:list,
        cartList,
      })
      this.getTotal()
      wx.setStorageSync('cart', cartList)
      console.log('购物车',cartList);
    },
    addNum(e){
      let id = e.currentTarget.dataset.id
      console.log('加',id);
      let list = this.data.foodList
      let  cartList = this.data.cartList
      list.forEach(item=>{
        // console.log('便利了',item.name);
        if(item._id == id){
          item.num ++; 
          if(cartList && cartList.length > 0){
            var result = cartList.find(cart=>{   //find方法 查询购物车数组cartList里面是否有当前点击的菜品
              return cart._id == id;
            })
            console.log('点击的菜品是否在购物车里',result);
            if(result){
              result.num = item.num
            }else{
              cartList.push(item)
            }
          }else{
            cartList.push(item)
          }
        }
      })
      console.log('当前菜品列foodList',list)
      console.log('购物车',cartList);
      this.setData({
        foodList:list,
        cartList,
      })
      this.getTotal()
       wx.setStorageSync('cart', cartList)
    },
    closeMask(){
      this.setData({
        isHide: true
      })
      
    },
    openCart(){
      this.setData({
        isHide: false
      })
    },
    //清空购物车
    clearCart(){
      let foodList = this.data.foodList
      foodList.forEach(item=>{
        item.num = 0
      })
      this.setData({
        foodList,
        cartList:[],
        totalMoney:0,
        totalNum:0
      })
      wx.setStorageSync('cart', null)
    },
    //删除购物车中某一项
    deleteItem(e){
      console.log(e.currentTarget.dataset.index);
      let index = e.currentTarget.dataset.index
      let cartList = this.data.cartList
      let foodList = this.data.foodList
      foodList.forEach(item=>{
        if(cartList[index]._id == item._id){
          item.num = 0
        }
      })
      cartList.splice('index',1)
      this.setData({
        cartList,
        foodList
      })
      this.getTotal()
      wx.setStorageSync('cart', cartList)
    },
    //去支付
    goPay(){
      wx.navigateTo({
        url: '/pages/pay/pay',
        
      })
    }
   
})